#include <iostream>
using namespace std;
#include <cstdlib>
#include <windows.h>
#include <conio.h>
#include "Lahan.h"
#include "player.h"
#include "Aqua.h"
#include "Bolt.h"
#include "Clash.h"
#include "Times.h"
#include "classmenu.h"


Lahan::Lahan()
{
    for(int i = 0; i < panjang; i++)
        for(int j = 0; j <= lebar; j++)
        {
            if (i == 0 || i == panjang-1 || j == 0 || j == lebar-1)
                Field[i][j].status = 0;
            else
                Field[i][j].status = 1;
        }
}
Lahan::Lahan(const Lahan& L)
{
    for(int i = 0; i < panjang; i++)
        for(int j = 0; j < lebar; j++)
        {
            Field[i][j].status = L.Field[i][j].status;
            Field[i][j].Plant = Field[i][j].Plant;
        }
}
Lahan::~Lahan()
{}
int Lahan::getStatus(int x, int y)
{
    return Field[x][y].status;
}
void Lahan::setStatus(int x, int y, int stat)
{
    Field[x][y].status = stat;
}
void Lahan::PrintLahanKosong()
{
    system("cls");
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
    T.PrintTimes();
    M.PrintStat();
    for(int i = 0; i < lebar; i++)
    {
        for(int j = 0; j < panjang; j++)
        {
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
            cout << "+-------";
        }
        cout << "+";
        cout << endl << '|';
        for(int j = 0; j < panjang; j++)
        {
            if(L.getStatus(i,j) == 0)
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x60);
            else
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x20);
            cout << "   " << ' ';
            cout << '\t';
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
            cout << '|';
        }
        cout << endl << '|';
        for(int j = 0; j < panjang; j++)
        {
            if(L.getStatus(i,j) == 0)
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x60);
            else
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x20);
            cout << "   " << ' ';
            cout << '\t';
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
            cout << '|';
        }
        cout << endl << '|';
        for(int j = 0; j < panjang; j++)
        {
            if(L.getStatus(i,j) == 0)
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x60);
            else
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x20);
            cout << "   " << ' ';
            cout << '\t';
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
            cout << '|';
        }
        cout <<  endl;
    }
    for(int j = 0; j < panjang; j++)
        {
            cout << "+-------";
        }
    cout << "+";
    cout << endl;
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
}
void Lahan::PrintLahan()
{
    Position PosPlayer = P.GetPosition();
    Position PosAqua;
    PosAqua.x = A.GetX();
    PosAqua.y = A.GetY();
    Position PosBolt;
    PosBolt.x = B.GetX();
    PosBolt.y = B.GetY();
    Position PosClash;
    PosClash.x = C.GetX();
    PosClash.y = C.GetY();

    T.UpdateTimes();
    M.UpdateStat();
    for (int j = 0 ; j < lebar; j++)
        for(int i = 0; i < panjang; i++)
        {
            if(L.getStatus(i,j) >= 3 && L.getStatus(i,j) <= 8)
            {

                switch(L.getTanaman(i,j).GetKodeTanaman())
                {
                    case 9 : //ChronoPlant
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2F); //WhiteGreen
                        break;
                    case 10: //ReynFruit
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x29); //LightBlue
                        break;
                    case 11: //Ichalmond
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x26); //Brown
                        break;
                    case 12: //Edondong
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2E); //Yellow
                        break;
                    case 13: //GreenAlgoe
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2A); //LightGreen
                        break;
                    case 14: //Hanselnut
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x24); //Red
                        break;
                    case 15: //AbelTree
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x23); //Cyan
                        break;
                    case 16: //Elmonade
                        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x25); //Purple
                        break;
                }
                PrintStatus(i,j);
            }
            else
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0xF6);
                PrintStatus(i,j);
            }
        }
    gotokoordinat(PosPlayer.x, PosPlayer.y);
    if(L.getStatus(PosPlayer.x,PosPlayer.y) == 0)
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x6F);
    else
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2F);
    cout << P.GetFace();
    if(A.GetStatus())
    {
        if(L.getStatus(PosAqua.x,PosAqua.y) == 0)
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x6F);
        else
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2F);
        gotokoordinat(PosAqua.x, PosAqua.y);
        cout << "A";
    }
    if(B.GetStatus())
    {
        if(L.getStatus(PosBolt.x,PosBolt.y) == 0)
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x6F);
        else
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2F);
        gotokoordinat(PosBolt.x, PosBolt.y);
        if (B.IsHolding())
        {
            char c = 225;
            cout << c;
        }
        else
            cout << "B";
    }
    if(C.GetStatus())
    {
        if(L.getStatus(PosClash.x,PosClash.y) == 0)
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x6F);
        else
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2F);
        gotokoordinat(PosClash.x, PosClash.y);
        cout << "C";
    }
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
    gotokoordinat(9,9);
    cout << endl;
}
Tanaman& Lahan::getTanaman(int x, int y)
{
    return *Field[x][y].Plant;
}
void Lahan::deleteTanaman (int x, int y)
{
    delete Field[x][y].Plant;
    Field[x][y].status = 1;
}
void Lahan::setTanaman(int x, int y, int jenis)
{
    switch (jenis)
    {
        case 9:
            Field[x][y].Plant = new ChronoPlant(x,y);
            break;
        case 10:
            Field[x][y].Plant = new ReynFruit(x,y);
            break;
        case 11:
            Field[x][y].Plant = new Ichalmond(x,y);
            break;
        case 12:
            Field[x][y].Plant = new Edondong(x,y);
            break;
        case 13:
            Field[x][y].Plant = new GreenAlgoe(x,y);
            break;
        case 14:
            Field[x][y].Plant = new Hanselnut(x,y);
            break;
        case 15:
            Field[x][y].Plant = new AbelTree(x,y);
            break;
        case 16:
            Field[x][y].Plant = new Elmonade(x,y);
            break;
    }
    Field[x][y].status = 3;
}

void Lahan::LoadTanaman(int x, int y, int jenis)
{
    switch (jenis)
    {
        case 9:
            Field[x][y].Plant = new ChronoPlant(x,y);
            break;
        case 10:
            Field[x][y].Plant = new ReynFruit(x,y);
            break;
        case 11:
            Field[x][y].Plant = new Ichalmond(x,y);
            break;
        case 12:
            Field[x][y].Plant = new Edondong(x,y);
            break;
        case 13:
            Field[x][y].Plant = new GreenAlgoe(x,y);
            break;
        case 14:
            Field[x][y].Plant = new Hanselnut(x,y);
            break;
        case 15:
            Field[x][y].Plant = new AbelTree(x,y);
            break;
        case 16:
            Field[x][y].Plant = new Elmonade(x,y);
            break;
    }
}

void Lahan::gotokoordinat(int x, int y)
{
    int RealX = 8*x + 4;
    int RealY = 4*y + 8;
    M.gotoxy(RealX,RealY);
}
void Lahan::PrintStatus(int x, int y)
{
    gotokoordinat(x,y);
    char c;
    if (x == 0 && y == 0) //Titik Portal
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x6F);
        c = 248;
    }
    else if (x == 8 && y == 0) //Titik penjualan
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x6F);
        c = 'X';
    }
    else if (Field[x][y].status == 0) //Unplantable
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x6F);
        c = 32;
    }
    else if (Field[x][y].status == 1) //Clear
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2F);
        c = 32;
    }
    else if (Field[x][y].status == 2) //Plowed
    {
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x26);
        c = 186;
    }
    else if (Field[x][y].status == 3) //Seed
    {
        c = 176;
    }
    else if (Field[x][y].status == 4) //Watered Seed
    {
        c = 1;
    }
    else if (Field[x][y].status == 5) //Planted
    {
        c = 6;
    }
    else if (Field[x][y].status == 6) //Watered Plant
    {
        c = 3;
    }
    else if (Field[x][y].status == 7) //Harvestable
    {
        c = 5;
    }
    else if (Field[x][y].status == 8) //Dead
    {
        c = 168;
    }
    cout << c;
}